<template>
	<view>

		<st-mc-framework :navTitle="ModuleTitle" @changeLevel="changeLevel"
			@AddModuleData_select='AddModuleData_select'>
			<template #workbench>
				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0 }" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="120">
					<template #BtnContent>
						<u-icon name="search" color="#fff" size="25px" @click="search"></u-icon>
					</template>
				</m-drag-button>

				<m-drag-button :style="{ opacity: BtnShow ? 1 : 0}" :isDock="true" :existTabBar="true"
					class="m-drag-button-view" :initialBottom="60">
					<template #BtnContent>
						<u-icon name="plus" color="#fff" size="25px" @click="add"></u-icon>
					</template>
				</m-drag-button>
				<view class="TabsBox">
					<view class="TabsBox_view">
						<view class="u-page__tag-item" v-for="(item, index) in BYStatusList" :key="index">
							<u-tag :text="item.name" :plain="!item.checked" :name="index" size="large"
								@click="radioClick(item,index)">
							</u-tag>
						</view>
					</view>
				</view>

			</template>
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>
								<th width='10%'>类型</th>
								<th width='25%'>模具</th>
								<th width='25%'>库位</th>
								<th width='25%'>操作时间</th>
								<th width='15%'>操作人</th>
							</tr>
						</table>
					</view>
					<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" class="z_paging">
						<view class="table">
							<table class="table-full">
								<tbody>
									<tr style="border: 1px solid #ccc;" v-for="(item,index) in dataList" :key="index"
										@click="itemClick(item,index)">
										<td width='10%'>
											<view class="ListTdDiv"><text>{{item.FType}}</text></view>
										</td>
										<td width='25%'>
											<view class="ListTdDiv"><text>{{item.MoludName}}</text></view>
										</td>
										<td width='25%'>
											<view class="ListTdDiv"><text>{{item.FStorageName}}</text></view>
										</td>

										<td width='25%'>
											<view class="ListTdDiv"><text>{{item.BillDate}}</text></view>
										</td>
										<td width='15%'>
											<view class="ListTdDiv"><text>{{item.EmplName}}</text></view>
										</td>

									</tr>
								</tbody>
							</table>
						</view>
					</z-paging>
				</view>
				<!-- 		<view class="add">
					<u-icon name="plus" color="#fff" size="30" @click="add"></u-icon>
				</view>
				<view class="search">
					<u-icon name="search" color="#fff" size="30" @click="search"></u-icon>
				</view> -->

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0"
					@change="changePopup">
					<view class="popup_content">
						<u-cell-group :border='false'>
							<u-cell title="模具 " :isLink="false">
								<view slot="value" style="width: 20%;">
									<u-input v-model="FormData.MoludName" placeholder="请输入" clearable></u-input>
								</view>
							</u-cell>
							<u-cell title="库位" :isLink="false">
								<view slot="value" style="width: 20%;">
									<u-input v-model="FormData.FStorageName" placeholder="请输入" clearable></u-input>
								</view>
							</u-cell>
							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>

						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>




			</template>

		</st-mc-framework>
		<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false'
			@confirm='pickerConfirm'></u-datetime-picker>
		<m-loading :text="loadingName" v-model="showMLoading" :showClose="false"
			maskBackgroundColor="rgba(0,0,0,0.3)"></m-loading>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime,
		formatDateTime,
		navigateMixin,
		baseAddModuleData_select,
		getchangeLevel
	} from '@/st_mes_zs/common/base.js';

	export default {
		mixins: [navigateMixin, baseAddModuleData_select],
		data() {
			return {
				dataList: [],

				FormData: {
					PageIndex: 1,
					PageSize: 30,
					FBDate: '',
					FEDate: '',
					EquipmentID: null,
					EquipmentName: '',
					FStorageName: '', //库位
					MoludName: '', //模具名称
					FType: '', //领用还是归还
				},
				APPFunID: 0,
				tabIndex: 0,
				showLeft: false,
				TimeName: '',
				show: false,
				value1: Number(new Date()),
				BYStatusShow: false,
				BYStatusList: [{
					name: '全部',
					id: '',
					checked: true
				}, {
					name: '领用',
					id: 'LY',
					checked: false
				}, {
					name: '归还',
					id: 'GH',
					checked: false
				}],
				disabled: false,
				ModuleTitle: '', //标题名称
				showMLoading: false, //是否加载
				loadingName: '正在加载中……', //加载名称
				BtnShow: false, //按钮显示
			}

		},
		onShow() {
			var that = this
			uni.$on('refreshList', () => {
				that.getList(); // 调用目标方法
			});
		},
		onUnload() {
			uni.$off('refreshList');
		},
		onReady() {
			var that = this
			uni.hideLoading();
			that.showMLoading = true
			const {
				GuyueModule,
				UserInfo,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;

			that.FWorkshopID = MesBasicInfo.FWorkshopID;
			that.ModuleTitle = appConfigInfo.ActiveModule.ModuleName;
			that.$nextTick(() => {
				setTimeout(() => {
					that.BtnShow = true;
				}, 500)
			})


		},
		onLoad(options) {
			var that = this
			that.$safeStatus({
				payload: '其他', // 要传递的数据
			});
			const {
				GuyueModule,
				appConfigInfo
			} = that.$store.state;
			const {
				MesBasicInfo
			} = GuyueModule;
			if (options.Dispatchitem) {
				const dispatchItem = JSON.parse(options.Dispatchitem);
				that.assignFormData(that.FormData, dispatchItem);
				return;
			}
			const formDefaults = {
				FBDate: getBefore7DateTime(),
				FEDate: getFormatDateTime(new Date()),
				EquipmentID: MesBasicInfo.warehouse.EquipmentID,
				EquipmentName: MesBasicInfo.warehouse.EquipmentName,
			};
			that.assignFormData(that.FormData, formDefaults);
		},
		methods: {
			// 创建表单赋值工具函数
			assignFormData(target, defaults = {}) {
				var that = this;
				Object.keys(defaults).forEach(key => {
					target[key] = defaults[key];
				});
				that.$forceUpdate();
			},

			radioClick(item, idx) {
				let that = this;
				this.BYStatusList.map((item, index) => {
					item.checked = index === idx ? true : false
				})
				that.FormData.FType = item.name;
				this.contents = [];
				this.$refs.paging.reload();
			},
			getList() {
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},

			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},

			pickerConfirm(e) {
				var time = getFormatDateTime(e.value)
				if (this.TimeName == '开始') {
					this.FormData.FBDate = time
				} else {
					this.FormData.FEDate = time
				}
				this.show = false
			},

			changePopup() {

			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {

				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			add() {
				let routes = getCurrentPages();
				var url = '/st_mes_zs/mold/moldstorageDetail'
				let obj = {
					path: 'manual',
					Name: '模具出入库详情',
					APPFunID: '模具出入库详情',
					length: routes.length + 1,
					url: url,
				}
				this.GonavigateTo(obj, url)
			},

			queryList(pageNo) {
				let that = this;
				that.FormData.PageIndex = pageNo
				try {
					that.Request.request('getMoldInOutStorageList', that.FormData, (ret) => {
						console.log(ret);
						if (ret.status == 200) {
							that.showMLoading = false
							ret.data.ds.map((item) => {
								item.BillDate = formatDateTime(item.BillDate)
							})
							that.$refs.paging.complete(ret.data.ds);
						} else {
							that.showMLoading = false
						}
					})
				} catch (e) {
					console.log(e)
					that.showMLoading = false
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			itemClick(item, index) {

				let routes = getCurrentPages();
				var url = `/st_mes_zs/mold/moldstorageDetail?StockID=${item.StockID}`
				let obj = {
					path: 'manual',
					Name: '模具出入库详情',
					APPFunID: '模具出入库详情',
					length: routes.length + 1,
					url: url,
				}
				this.GonavigateTo(obj, url)
			},

			GonavigateTo(obj, url) {
				let that = this;
				var initData = JSON.parse(JSON.stringify(that.$store.state.GuyueModule.ModuleData_select));
				var parentData = initData[initData.length - 1]
				parentData.url = `/st_mes_zs/mold/moldstoragelist?Dispatchitem=${JSON.stringify(that.FormData)}`;
				that.$modifyParentData(parentData);
				this.$safeNavigate({
					targetUrl: url, // 目标页面路径
					payload: obj, // 要传递的数据
				});
			},
			async AddModuleData_select(res) {
				try {
					const backDelta = await this.GetModuleData_select(res);
				} catch (error) {
					console.error('返回操作失败:');
				}
			},

			changeLevel(list, data, row) {
				console.log(list);
				console.log(data);
				console.log(row);
				uni.redirectTo({
					url: data.url || '/st_mes_zs/index/index',
					animationType: 'none'
				});
			},
		},


		onNavigationBarButtonTap(e) {
			if (this.showLeft) {
				this.$refs.showLeft.close()
			} else {
				this.$refs.showLeft.open()
			}
		},
		// app端拦截返回事件 ，仅app端生效
		onBackPress() {
			if (this.showRight || this.showLeft) {
				this.$refs.showLeft.close()
				this.$refs.showRight.close()
				return true
			}
		}
	}
</script>

<style lang="scss" >
	@import "../css/index.css";


	.table table {

		tr td {
			input {
				width: 100%;
				background-color: #e7e7e7 !important;
			}
		}

	}
</style>